package com.whmnx.chitchat.ui.chat;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;

import com.orhanobut.logger.Logger;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.BaseBean;
import com.whmnx.chitchat.bean.ImgResultBean;
import com.whmnx.chitchat.bean.UserBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.ui.my.ChangeInfoActivity;
import com.whmnx.chitchat.ui.my.MyInfoActivity;
import com.whmnx.chitchat.utils.PhotoUtil;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/8/27 0027 18:47
 * version: 1.0
 *
 * @author Administrator
 */
public class ChatPerSettingActivity extends BaseActivity {
    @BindView(R.id.btnSwitch)
    Switch btnSwitch;
    @BindView(R.id.tv_nickName)
    TextView tvNickName;

    private String phone,remark;
    String url;

    boolean flag = false;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.chat_per_activity);
        setTitle("资料设置");
    }

    @Override
    protected void loadData() {
        getIsFriend();

        btnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (flag){
                    UItils.showToastSafe("还不是常用好友");
                    return;
                }
                if (!buttonView.isPressed()) return;
                Map<String, Object> params = new HashMap<>();
                params.put("Mobile", SPUtil.getString(Constants.PHONE));
                params.put("FriendMobile", phone);
                if (isChecked) {
                    url = Constants.GETBLOCKFRIEND;
                } else {
                    url = Constants.GETREMOVEBLOCKFRIEND;
                }
                HttpClient.get(ChatPerSettingActivity.this, url, params, new CallBack<UserBean>() {
                    @Override
                    public void onSuccess(UserBean loginBean) {

                    }
                });
            }
        });
    }

    private void getIsFriend() {
        String id = getIntent().getStringExtra("ToUserID");
        Map<String, Object> params = new HashMap<>();
        params.put("ToUserID", id);
        params.put("UserID", SPUtil.getString(Constants.USERID));
        HttpClient.get(ChatPerSettingActivity.this, Constants.GETISFRIEND, params, new CallBack<List<UserBean>>() {
            @Override
            public void onSuccess(List<UserBean> loginBean) {
                if (loginBean.size() == 0){
                    flag = true;
                    return;
                }
                if (loginBean.get(0).getStatus() == 3) {
                    btnSwitch.setChecked(true);
                } else {
                    btnSwitch.setChecked(false);
                }
                phone = loginBean.get(0).Moblie;
                remark = loginBean.get(0).Remarks;
                tvNickName.setText(remark);
            }
        });
    }



    @OnClick(R.id.ll_nick)
    public void onViewClicked() {  //.putExtra("Remarks", )
        if (flag){
            UItils.showToastSafe("还不是常用好友");
            return;
        }
        Intent intentCamera = new Intent();
        intentCamera.putExtra("type", 13);
        intentCamera.putExtra("FriendMobile", phone);
        intentCamera.putExtra("Remarks", remark);
        intentCamera.setClass(this, ChangeInfoActivity.class);
        startActivityForResult(intentCamera, 1000);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1000:
                  String s = data.getStringExtra("name");
                    tvNickName.setText(data.getStringExtra("name"));
                    MessageEvent messageEvent = new MessageEvent();
                    messageEvent.setMessageKey(EventConstants.NICKPOP);
                    messageEvent.setMessageValue(s);
                    EventBus.getDefault().post(messageEvent);
                    break;
                default:
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
